﻿Public Class ucManageStaffAuthorize
    Inherits System.Web.UI.UserControl

    Event Alert(ByVal msg As String)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim dt As DataTable = New DataTable
            dt = BL.Authentication.LoadUserIDInfo(Request.QueryString("officerappid"))

            If Not IsNothing(dt) AndAlso dt.Rows.Count > 0 Then
                OFFICER_APP_ID.Text = dt.Rows(0).Item("OFFICER_APP_ID")
                If Not IsDBNull(dt.Rows(0).Item("OFFICER_FIRSTNAME_THA")) Then OFFICER_FIRSTNAME_THA.Text = dt.Rows(0).Item("OFFICER_FIRSTNAME_THA")
                If Not IsDBNull(dt.Rows(0).Item("OFFICER_LASTNAME_THA")) Then OFFICER_LASTNAME_THA.Text = dt.Rows(0).Item("OFFICER_LASTNAME_THA")
                If Not IsDBNull(dt.Rows(0).Item("USER_ID")) Then USER_ID.Text = dt.Rows(0).Item("USER_ID")

                gvPrivilege.DataSource = BL.Authentication.LoadActiveFunction(USER_ID.Text)
                gvPrivilege.DataBind()
                gvRole.DataSource = BL.Authentication.LoadActiveRole(USER_ID.Text)
                gvRole.DataBind()
            Else
                OFFICER_APP_ID.Text = "ไม่พบข้อมูล User ID ของ " & Request.QueryString("officerappid")
            End If
        End If
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        Dim oUserRole As New List(Of Entity.UserRowItem)
        Dim oUserPrivilege As New List(Of Entity.UserPrivilegeItem)

        For iRow As Int16 = 0 To gvRole.Rows.Count - 1
            Dim grdRow As GridViewRow = gvRole.Rows(iRow)

            Dim lblRoleAuth As Label = CType(grdRow.FindControl("lblRoleAuth"), Label)
            Dim lblRoleID As Label = CType(grdRow.FindControl("lblRoleID"), Label)
            Dim chkRoleID As CheckBox = CType(grdRow.FindControl("chkRoleID"), CheckBox)
            If chkRoleID.Checked Then
                Dim oUserRoleDetail As Entity.UserRowItem
                oUserRoleDetail = New Entity.UserRowItem

                oUserRoleDetail.role_id = lblRoleID.Text.Trim
                oUserRoleDetail.user_id = USER_ID.Text.Trim

                oUserRole.Add(oUserRoleDetail)
            End If
        Next

        For iRow As Int16 = 0 To gvPrivilege.Rows.Count - 1
            Dim grdRow As GridViewRow = gvPrivilege.Rows(iRow)

            Dim lblFunctionAuth As Label = CType(grdRow.FindControl("lblFunctionAuth"), Label)
            Dim lblFunctionID As Label = CType(grdRow.FindControl("lblFunctionID"), Label)
            Dim chkFunctionID As CheckBox = CType(grdRow.FindControl("chkFunctionID"), CheckBox)
            If chkFunctionID.Checked Then
                Dim oUserPriDetail As Entity.UserPrivilegeItem
                oUserPriDetail = New Entity.UserPrivilegeItem

                oUserPriDetail.function_id = lblFunctionID.Text.Trim
                oUserPriDetail.user_id = USER_ID.Text.Trim
                oUserPriDetail.privilege_id = "F"

                oUserPrivilege.Add(oUserPriDetail)
            End If
        Next

        Dim oResult As Boolean
        oResult = BL.Authentication.UpdateUserAuthorize(oUserRole, oUserPrivilege, USER_ID.Text.Trim)

        If oResult Then
            GlobalApp.DepositMessage = "SUCCESS\nบันทึกการเปลี่ยนแปลงสิทธิ์ผู้ใช้งานเสร็จเรียบร้อยแล้ว"
        Else
            GlobalApp.DepositMessage = "FAIL!\nบันทึกเปลี่ยนแปลงสิทธิ์ผู้ใช้งานไม่สำเร็จ รองใหม่อีกครั้ง"
        End If

        Try
            Response.Redirect("~/EditStaff.aspx", False)
        Catch ex As Exception
            RaiseEvent Alert(ex.Message)
        End Try
    End Sub

    Private Sub LoadData(ByVal datasource As Entity.OfficerInfoItem)

        If datasource Is Nothing Then
            Throw New Exception("ไม่พบข้อมูล Staff ที่นำมาแสดงผล")
        End If

        Dim registerInfo As Entity.OfficerInfoItem
        registerInfo = datasource
        With registerInfo

        End With
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
        Response.Redirect("~/EditStaff.aspx", False)
    End Sub

    Private idxRow As Int32 = 0
    Private Sub gvRole_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvRole.RowDataBound
        Try
            Dim lblRoleAuth As Label = CType(e.Row.FindControl("lblRoleAuth"), Label)
            Dim chkRoleID As CheckBox = CType(e.Row.FindControl("chkRoleID"), CheckBox)
            If Convert.ToInt32(lblRoleAuth.Text.Trim) > 0 Then
                chkRoleID.Checked = True
            Else
                chkRoleID.Checked = False
            End If

            idxRow += 1
        Catch ex As Exception

        End Try
    End Sub

    Private Sub gvPrivilege_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvPrivilege.RowDataBound
        Try
            Dim lblFunctionAuth As Label = CType(e.Row.FindControl("lblFunctionAuth"), Label)
            Dim chkFunctionID As CheckBox = CType(e.Row.FindControl("chkFunctionID"), CheckBox)
            If Convert.ToInt32(lblFunctionAuth.Text.Trim) > 0 Then
                chkFunctionID.Checked = True
            Else
                chkFunctionID.Checked = False
            End If

            idxRow += 1
        Catch ex As Exception

        End Try
    End Sub

End Class